<?php
namespace app\controller;

use app\BaseController;
use think\App;
use think\cache\driver\Redis;
use think\facade\Db;

/**
 * Class Rank
 * @package app\controller
 *  redis hash 用来存对象
 */
class HashR extends BaseController
{
    private $redis;
    private $key;
    /**
     * @var string
     */
    private $rank;

    public function __construct(App $app)
    {
        $this->redis = new Redis();
        $this->key = 'order';
        $this->rank = 'rank';
        parent::__construct($app);
    }

    // 初始化
    public function init(){
        $store=10000; // 库存50
        $redis=$this->redis; //接入redis
        $redis->del($this->key); // 删除库存列表

        $res = Db::name("xn_admin")->find();//查找商品
        $redis->hmset('admin_info',$res);
    }
    ///获取 获取hash 里面的值
    public function hget(){

        $this->redis->hset('order','amount',2799.00);
        $res = $this->redis->hgetall('order');

        echo "<pre>";
        print_r($res);
    }

    public function testhmset(){
        $redis = $this->redis;
        $redis->hMSet('h',
                    array(
                         'f1' => 'v1',
                         'f2' => 'v2',
                         'f3' => 'v3',
                         'f4' => 'v4',
                    ));

         var_dump($redis->hGetAll('h'));
    }
    ///模拟取消显示的ids
    public function huid(){
        $uid  = 2;
        $course_ids = '1,2,3,4,5,6';
        $key  = "hidden_course_ids_".$uid ;
        $this->redis->hset($key,$uid,$course_ids);
        $res = $this->redis->hgetall($key);
        return json($res);
    }
    ///模拟取消显示的ids
    public function hiddenCourseIds(){

        $key  = "hidden_course_ids_all" ;
        $list = [];
        for($i=1;$i<50000;$i++){
            $uid = mt_rand(10000,50000);
            $list[$uid] = json_encode([$i+1,$i*3,$i+66,$i*44,$i+4,$i+5]);
        }


        $this->redis->hmset($key,$list);
        //$res = $this->redis->hgetall($key);
        $res = $this->redis->hget($key,"9839");
        return json($res);
        return json(json_decode($res,true));
    }


    // 给某个成员加分
    function zincrby(){
        $score = mt_rand(0,10000);
        $member = '王五'.$score;
        $this->redis->zincrby($this->rank,$score*3,$member);
    }



    // 返回某个人的排名   从大到小排列  zrevrank   从小到达分数排列 zrank
    function getSomebodyRank(){
        $member = '王五'.mt_rand(0,10000);
        #$member = '王五0';
        $sbScore = $this->redis->zscore($this->rank,$member);
        $sbRank = $this->redis->zrevrank($this->rank,$member);
        echo $member.'分数为：'.$sbScore.',排名为：'.$sbRank;
    }
}
